<template>
  <div class="payment-page">
    <div class="container">
      <div class="warning">
        <table border="0" style="width:1000px; text-align:center;" cellspacing="0" cellpadding="0">
          <tbody>
            <tr height="35">
              <td style="font-size:18px;">
                感谢您在本店购物！您的订单已提交成功，请记住您的订单号: 
                <font color="#ff4e00">{{ orderInfo.serialNumber }}</font>
              </td>
            </tr>
            <tr>
              <td style="font-size:14px; font-family:'宋体'; padding:10px 0 20px 0; border-bottom:1px solid #b6b6b6;">
                您选定的支付方式为: <font color="#ff4e00">支付宝</font>； &nbsp; &nbsp;
                您的应付款金额为: <font color="#ff4e00">￥{{ orderInfo.cost }}</font>
              </td>
            </tr>
            <tr>
              <td style="padding:20px 0 30px 0; font-family:'宋体';">
                支付宝网站(www.alipay.com) 是国内先进的网上支付平台。<br />
                支付宝收款接口：在线即可开通，零预付，免年费，单笔阶梯费率，无流量限制。<br />
                <a href="#" style="color:#ff4e00;">立即在线申请</a>
              </td>
            </tr>
            <tr>
              <td>
                <div class="btn_u" style="margin:0 auto; padding:0 20px; width:120px;">
                  <a href="#" @click.prevent="proceedToPayment">立即使用支付宝支付</a>
                </div>
                <br /><br />
                <a href="#" @click.prevent="goToHome">首页</a> &nbsp; &nbsp; 
                <a href="#" @click.prevent="goToUserCenter">用户中心</a>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
import { payOrder, getOrderDetails } from '@/api/orderApi'

export default {
  name: 'AlipayPayment',
  data() {
    return {
      orderInfo: {
        serialNumber: '',
        cost: '',
        id: ''
      },
      paymentUrl: ''
    }
  },
  async created() {
    // 从路由参数获取订单信息
    this.orderInfo.serialNumber = this.$route.params.orderId || ''
    // 从路由参数获取订单金额
    this.orderInfo.cost = this.$route.params.amount || '0.00'
    
    // 获取订单详细信息
    if (this.orderInfo.serialNumber) {
      await this.fetchOrderDetails(this.orderInfo.serialNumber)
    }
  },
  methods: {
    // 获取订单详情
    async fetchOrderDetails(orderId) {
      try {
        const response = await getOrderDetails(orderId)
        if (response.status === 200 && response.data) {
          this.orderInfo = {
            ...this.orderInfo,
            ...response.data
          }
        }
      } catch (error) {
        console.error('获取订单详情失败:', error)
      }
    },
    
    // 跳转到首页
    goToHome() {
      this.$router.push({ name: 'Home' })
    },
    
    // 跳转到用户中心
    goToUserCenter() {
      this.$router.push({ name: 'UserCenter' })
    },
    
    // 执行支付
    async proceedToPayment() {
      try {
        // 调用支付接口
        const response = await payOrder(this.orderInfo.serialNumber)
        
        // 如果返回的是HTML内容（支付宝支付页面），则直接在新窗口打开
        if (response && typeof response === 'string' && response.includes('<form')) {
          // 创建临时div来存放支付宝表单
          const div = document.createElement('div')
          div.innerHTML = response
          document.body.appendChild(div)
          
          // 提交表单
          const form = div.querySelector('form')
          if (form) {
            form.submit()
          }
        } else {
          this.$message.error('支付失败')
        }
      } catch (error) {
        console.error('支付失败:', error)
        this.$message.error('支付失败: ' + (error.message || '网络错误'))
      }
    }
  }
}
</script>

<style scoped>
.payment-page {
  padding: 20px;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.warning {
  border: 1px solid #ddd;
  padding: 20px;
  background-color: #fff;
}

.warning table {
  width: 100%;
  text-align: center;
}

.btn_u {
  display: inline-block;
  padding: 10px 20px;
  background-color: #ff4e00;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  margin: 10px 0;
}

.btn_u a {
  color: white;
  text-decoration: none;
  font-weight: bold;
}

.btn_u:hover {
  background-color: #e04500;
}
</style>